// vue2 路由配置
// import VueRouter from 'vue-router'
// const router  = new VueRouter({
//   mode: 'history' // hash history,
//   routes: [
//     { path: '/home', component: () => import('@/views/Home/index.vue')}
//   ]
// })
// export default router

// vue3 路由配置
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

// 引入 NProgress 及其样式
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 关闭 NProgress 的loading效果
NProgress.configure({
  showSpinner: false
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue')
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: () => import('@/views/Home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: 'article',
          component: () => import('@/views/Article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: 'notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: {
            title: '消息通知'
          }
        },
        {
          path: 'user',
          component: () => import('@/views/User/index.vue'),
          meta: {
            title: '个人中心'
          }
        }
      ]
    }
  ]
})
// 全局前置守卫
router.beforeEach((to, from) => {
  NProgress.start()
  // return '路由地址' 跳转指定地址  next("路由地址")
  // return true 或者不 retrun 就是放行        next()
  // return false 就是不放行                  next(false)
  const userStore = useUserStore()
  // 没有token 并且要去页面的还不是白名单页面，给他跳转到登录页
  const wihteList = ['/login']
  if (!userStore.user?.token && !wihteList.includes(to.path)) return '/login'
  // 其他情况一律放行
})
// 全局后置守卫
router.afterEach((to, from) => {
  console.log(to.meta.title)
  // 关闭进度条
  NProgress.done()
  // 设置页面标题
  document.title = `优医问诊-${to.meta.title}`
})
export default router
